<template>
  <view>
    <!--转授企业管理员-->
    <uni-popup ref="modifyCompanyAdmin">
      <uni-popup-dialog
          type="info"
          cancelText="取消"
          confirmText="确认"
          title="转授企业管理员"
          @close="cancelModifyCompanyAdmin"
          @confirm="updateAdminSubmitForm"
          before-close>
        <template v-slot="content">
          <view class="tip">
            <i class="iconfont colorIcon" style="margin-right: 10rpx;font-size: 26rpx;">&#xe70f;</i>
            您正在进行管理员身份转授，为确保账户安全，请根据要求完成转授操作。
          </view>
          <uni-forms
              ref="form"
              :label-width="70" type="line" label-align="right"
              class="paddingAll">
            <view style="padding:15rpx 0;margin-bottom: 10rpx;">
              <span style="font-weight: bold;">管理员信息</span>
              <span style="float:right;">
              <uni-tag type="primary" text="选择转授员工" @click="toSelectStaff"/>
            </span>
            </view>
            <uni-forms-item label="转授对象" name="companyName">
              <uni-easyinput placeholder="从企业内部组织架构选择转授员工" type="text" v-model="selectStaff.name" disabled/>
            </uni-forms-item>
            <uni-forms-item label="身份证号" name="idNumber">
              <uni-easyinput v-model="selectStaff.idNumberHide" type="text" placeholder="选择转授员工后系统自动填写" disabled/>
            </uni-forms-item>
            <uni-forms-item label="手机号码" name="mobile">
              <uni-easyinput v-model="selectStaff.mobileHide" type="text" placeholder="选择转授员工后系统自动填写" disabled/>
            </uni-forms-item>
          </uni-forms>
          <view class="show-text">
            <i class="iconfont colorIcon" style="font-size: 26rpx;margin-right: 10rpx;">&#xe70f;</i>
            <span>提醒：点击“提交”按钮，系统将变更当前企业管理员，请知晓以下信息：</span>
            <p style="margin-top: 10rpx;">1、转授成功后原管理员权限将自动失效。</p>
            <p>2、新管理员需要重新获得单位法人或法定代表人的全业务授权。</p>
          </view>
        </template>
      </uni-popup-dialog>
    </uni-popup>
    <!--待签署授权协议-->
    <uni-popup ref="modifyCompanyAdminRecord">
      <uni-popup-dialog
          type="info"
          cancelText="撤销转授"
          confirmText="重新发送"
          title="待签署授权协议"
          @close="revodeCompanyAdminChange"
          @confirm="resendNotice"
          before-close>
        <template v-slot="content">
          <uni-icons style="float: right;margin: -80rpx 10rpx 0 0;" type="closeempty" size="18"
                     @click="cancelModifyCompanyAdminRecord"/>
          <view class="show-text" style="text-align: center;">
            <i class="iconfont themeColor" style="font-size:100rpx">&#xe66a;</i>
            <p>即将转授至新管理员：{{ companyAdminChangeRecord.adminNewName }}（{{ companyAdminChangeRecord.adminNewMobile }}）</p>
            <p style="font-weight: 550;">待单位法定代表人签署《法定代表人全业务授权书》，请耐心等待！</p>
            <p style="color: #999999">
              若法定代表人未收到系统发送的通知短信，请复制授权书链接，通过微信、钉钉等方式发送，待法定代表人签署后，新管理员获得授权，转授成功。
            </p>
            <view>
              <p>法定代表人：{{ companyAdminChangeRecord.legalPerson }} </p>
              <p>手机号码：{{ companyAdminChangeRecord.legalPersonMobile }}</p>
              <p>发送时间：{{ companyAdminChangeRecord.createTime }}</p>
              <p>授权链接：{{ companyAdminChangeRecord.createTime }}</p>
            </view>
            <p class="themeColor"
               style="text-align: center; font-size: 28rpx;font-weight: 550;"
               @click="copyLegalSignUrl"
            >点击复制授权链接</p>
          </view>
        </template>
      </uni-popup-dialog>
    </uni-popup>

    <!-- 人员选择 -->
    <select-staff ref="selectStaff" @success="toSetSelectStaff"></select-staff>
  </view>
</template>

<script>
import {
  getWaitingCompanyAdminChange,
  resendCompanyAdminChangeRecordNotice,
  revodeCompanyAdminChange,
  updateAdminSendContract
} from "@/api/ec/console/index";
import selectStaff from "./selectStaff";

export default {
  components: {selectStaff},
  props: {},
  data() {
    return {
      // 企业信息
      companyInfo: this.$store.getters.companyInfo,
      // 已选人员数据
      selectStaff: {},
      // 企业管理员变更记录
      companyAdminChangeRecord: {},
    };
  },
  methods: {
    /** 打开弹出框 */
    toOpenDialog() {
      this.selectStaff = {};
      this.companyAdminChangeRecord = {};
      this.getWaitingInfo();
    },
    /** 转授企业管理员取消按钮 */
    cancelModifyCompanyAdmin() {
      this.$refs.modifyCompanyAdmin.close()
    },
    /** 待签署授权协议关闭按钮 */
    cancelModifyCompanyAdminRecord() {
      this.$refs.modifyCompanyAdminRecord.close()
    },
    // =================================================================================================================
    // ==== 选择授权对象
    // =================================================================================================================
    /** 选择授权对象 */
    toSelectStaff() {
      this.$refs.selectStaff.toOpenDialog(0, this.selectStaff);
    },
    /** 设置已选则授权对象 */
    toSetSelectStaff(selectStaff) {
      this.selectStaff = selectStaff;
    },
    /** 变更企业信息,确定按钮 */
    updateAdminSubmitForm() {
      if (this.selectStaff.id == null || this.selectStaff.id == 0) {
        this.$modal.msgError("请先选择转授对象！");
        return;
      }
      let newAdminId = this.selectStaff.id;
      const param = {newAdminId}
      uni.showLoading({title: '加载中...', mask: true});
      updateAdminSendContract(param).then(response => {
        uni.hideLoading()
        if (this.companyInfo.legalPersonId != newAdminId) {
          this.cancelModifyCompanyAdmin();
          setTimeout(() => {
            this.getWaitingInfo();
          }, 200);
        } else {
          let _this = this
          uni.showModal({
            title: '系统提示',
            confirmColor: '#00a660',
            confirmText: '确定',
            content: '管理员转授成功，请重新登录系统！',
            success: function (res) {
              if (res.confirm) {
                _this.$store.dispatch('LogOut').then(() => {
                  uni.reLaunch({url: '/pages/index'});
                })
              }
            }
          });
        }
      }).catch(() => {
        uni.hideLoading()
      });
    },
    /** 根据公司id查询待签署的数据 */
    getWaitingInfo() {
      // 获取管理员变更记录
      uni.showLoading({title: '加载中...', mask: true});
      getWaitingCompanyAdminChange().then((response) => {
        uni.hideLoading()
        if (typeof (response.data) == "undefined") {
          // 无待签署的变更记录，打开短信验证窗口 2023年7月4日  转授管理员暂时不验证管理员的手机号 因法人可能操作转授 不需经过管理员的验证码
          this.$refs.modifyCompanyAdmin.open('center')
        } else {
          // 有待签署的变更记录，打开流程记录显示窗口
          this.companyAdminChangeRecord = response.data;
          this.$refs.modifyCompanyAdminRecord.open('center');
        }
      });
    },
    /** 撤销企业管理员变更 */
    revodeCompanyAdminChange() {
      // 获取管理员变更记录
      uni.showLoading({title: '加载中...', mask: true});
      revodeCompanyAdminChange().then((response) => {
        this.$modal.msgSuccess("撤销成功！");
        uni.hideLoading()
        this.cancelModifyCompanyAdminRecord()
      }).catch(() => {
        uni.hideLoading()
      });
    },
    /** 重新发送企业管理员变更短信 */
    resendNotice() {
      uni.showLoading({title: '加载中...', mask: true});
      resendCompanyAdminChangeRecordNotice().then((response) => {
        uni.hideLoading()
        this.$modal.msgSuccess("发送成功！");
      }).catch(() => {
        uni.hideLoading()
      });
    },
    /** 复制授权链接 */
    copyLegalSignUrl() {
      this.$copyText(this.companyAdminChangeRecord.adminNewName + "通知您签署《电子签约平台全业务授权书》，点击签署https://t.lksign.cn/" + this.companyAdminChangeRecord.signUrl).then(
          () => {
            this.$modal.msgSuccess("授权链接已复制到剪切板，可粘贴。");
          },
          () => {
            this.$modal.msgError("复制失败！");
          }
      );
    },
  }
};
</script>
<style scoped lang="scss">

::v-deep .uni-popup-dialog {
  width: 90%;
}

::v-deep .uni-dialog-content {
  padding: 20rpx;
  display: block;
}

.tip {
  background: #E6F6EF;
  padding: 15rpx;
  font-size: 22rpx;
  font-weight: 400;
  color: #555b65;
  line-height: 35rpx;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: baseline;
  -ms-flex-align: baseline;
  align-items: baseline;
  margin-bottom: 25rpx;
}

.show-text {
  color: #666;
  padding: 15rpx;

  p {
    text-align: left;
    font-size: 26rpx;
    text-indent: 1rem;
    margin-top: 25rpx;
  }
}

</style>
